'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:TaskreviewCtrl
 * @description
 * # TaskreviewCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('TaskreviewCtrl', function($scope, $routeParams, $filter, $rootScope, logList, TaskReviewService, Util, openApi, submitAudit) {
        // 详情接口，审批记录接口，提交审核接口
        var logUrl, detailDataUrl, submiturl;
        var TVtype;
        TVtype = Util.getTVType();
        if (TVtype == Util.CHECK) { //普通审批
            detailDataUrl = openApi.allDetailApi;
            logUrl = openApi.myProcessLog;
        } else if (TVtype == Util.AUDIT) {
            detailDataUrl = openApi.getTaskInfo;
            logUrl = openApi.myProcessLogA;
            submiturl = openApi.completeAudit
        } else if (TVtype == Util.SYNCAUDIT) { //并行审批
            detailDataUrl = openApi.getTaskInfo;
            logUrl = openApi.myProcessLogA;
            submiturl = openApi.completeSyncAduit
        } else if (TVtype == Util.CAUDIT) { //自定义流程审批
            detailDataUrl = openApi.getTaskInfo;
            logUrl = openApi.myProcessLogA;
            submiturl = openApi.completeCaduitObject
        }
        $scope.auditText = "";
        $scope.check = function(item) {
            // $scope.auditText = item.name;//不显示默认值
            $scope.operationId = item.id;
            $scope.showDailog = true;
            $rootScope.showMasker = true;
        };
        $scope.confirm = function() {
            $scope.showDailog = false;
            $rootScope.showMasker = false;
            completeAudit();
        };
        $scope.cancel = function() {
            console.log("cancel")
            $scope.showDailog = false;
            $rootScope.showMasker = false;
        };
        $scope.hrefLink = function(href) {
            // ios & android are opened in the different way
            if (window.device && window.device.platform == "iOS") {
                window.open(href, '_blank', 'location=yes');
            } else {
                window.open(href, '_system', 'location=no');
            }
        }
        var completeAudit = function() {
            var param = {};
            param.taskId = $scope.TaskReview.result.taskId;
            param.operationId = $scope.operationId;
            param.attachmentDesc = $scope.auditText;
            param.attachmentName = $scope.attachmentName||"";
            param.url = submiturl;
            new submitAudit(param);

        };
        // 获取回显的详细信息
        var getDetailData = function(callback) {
            var options = {};
            options.param = {};
            options.param.processInstanceId = $routeParams.id;
            options.param.taskId = $routeParams.id;
            options.url = detailDataUrl;
            options.callback = callback;
            var TaskReview = new TaskReviewService(options);
            $scope.TaskReview = TaskReview;
        };
        // 获取日志信息
        var getLogListData = function() {
            var param = {}
            var options = {};
            options.param = {};

            options.url = logUrl;
            options.param.processId = $routeParams.id;
            options.param.taskId = $routeParams.id;
            $scope.logData = new logList(options);
        };
        var init = function() {
            if (TVtype == Util.CHECK) {
                getDetailData(function(data, form_key) {
                    // 设置formKey用来进入关联模块
                    $scope.form_key = form_key;
                });

            } else if (TVtype == Util.AUDIT || TVtype == Util.SYNCAUDIT || TVtype == Util.CAUDIT) {
                getDetailData(function(data, form_key) {
                    // 根据返回数据显示按钮
                    Util.showAudit({
                        scope: $scope
                    });
                    Util.showAuditDailog({
                        scope: $scope
                    });
                    // 显示添加附件
                    Util.showAddFile({
                        scope: $scope
                    });
                });
            }
            getLogListData();
        };
        // 初始化
        init();
    });
